public class Inventory extends GameStage {
  boolean ready= false;
  boolean backtomap = false;
  boolean invselected = false;
  boolean eselected = false;
  PImage img;
  ArrayList<Button> bagmenu;
  ArrayList<Button> equippedmenu;
  Button back;
  Player p;

  public Inventory (Player p1) {
    img = loadImage("caveinventory.png");
    background(img);
    img = loadImage("buttonpic.png");   
    p = p1;
    image(img, 32, 68);
    image(img, 197, 68);
    image(img, 32, 194);
    image(img, 197, 194);
    image(img, 32, 307);
    image(img, 197, 307);
    image(img, 113, 418);
    image(img, 388, 34);
    image(img, 388, 102);
    image(img, 388, 176);
    image(img, 388, 250);
    image(img, 388, 319);
    image(img, 388, 386);
    image(img, 388, 456);
    image(img, 388, 520);
    image(img, 518, 34);
    image(img, 518, 102);
    image(img, 518, 176);
    image(img, 518, 250);
    image(img, 518, 319);
    image(img, 518, 386);
    image(img, 518, 456);
    image(img, 518, 520);
    image(img, 658, 34);
    image(img, 658, 102);
    image(img, 658, 176);
    image(img, 658, 250);
    image(img, 658, 319);
    image(img, 658, 386);
    image(img, 658, 456);
    image(img, 658, 520);
    image(img, 5, 5, img.width/2, img.height/2);
    fill(0);
    text("Back", 12, 22);
    back = new Button(5, 5, img.width/2, img.height/2); 
    bagmenu = new ArrayList<Button>();
    equippedmenu = new ArrayList<Button>();
    equippedmenu.add(new Button(32, 68, 100, 50));
    equippedmenu.add(new Button(197, 68, 100, 50));
    equippedmenu.add(new Button(32, 194, 100, 50));
    equippedmenu.add(new Button(197, 194, 100, 50));
    equippedmenu.add(new Button(32, 307, 100, 50));
    equippedmenu.add(new Button(197, 307, 100, 50));
    equippedmenu.add(new Button(113, 418, 100, 50));
    bagmenu.add(new Button(388, 34, 100, 50));
    bagmenu.add(new Button(388, 102, 100, 50));
    bagmenu.add(new Button(388, 176, 100, 50));
    bagmenu.add(new Button(388, 250, 100, 50));
    bagmenu.add(new Button(388, 319, 100, 50));
    bagmenu.add(new Button(388, 386, 100, 50));
    bagmenu.add(new Button(388, 456, 100, 50));
    bagmenu.add(new Button(388, 520, 100, 50));
    bagmenu.add(new Button(518, 34, 100, 50));
    bagmenu.add(new Button(518, 102, 100, 50));
    bagmenu.add(new Button(518, 176, 100, 50));
    bagmenu.add(new Button(518, 250, 100, 50));
    bagmenu.add(new Button(518, 319, 100, 50));
    bagmenu.add(new Button(518, 386, 100, 50));
    bagmenu.add(new Button(518, 456, 100, 50));
    bagmenu.add(new Button(518, 520, 100, 50));
    bagmenu.add(new Button(658, 34, 100, 50));
    bagmenu.add(new Button(658, 102, 100, 50));
    bagmenu.add(new Button(658, 176, 100, 50));
    bagmenu.add(new Button(658, 250, 100, 50));
    bagmenu.add(new Button(658, 319, 100, 50));
    bagmenu.add(new Button(658, 386, 100, 50));
    bagmenu.add(new Button(658, 456, 100, 50));
    bagmenu.add(new Button(658, 520, 100, 50));
    reload();
  }

  public void mouseAct(int x, int y) {
    if (ready) {
      if (back.isClicked(x, y)) {
        backtomap = true;
      }
      else {
        int j = 0;
        for (Button b : bagmenu) {
          if (!invselected && b.isClicked(x, y)) {
            if (eselected) {
              
              for (Button z : equippedmenu) {
                if (z.chosen) {
                  if (z.armor== null) {                    
                    z.armor = new InvSlot(b.armor.item); 
                    b.ignore = true;
                    z.ignore = false;                    
                    img = loadImage("buttonpic.png");
                    eselected = false;
                    invselected = false;
                    b.chosen = false;
                    z.chosen = false;
                    image(img, b.xcor, b.ycor);
                    image(img, b.xcor, b.ycor);
                    image(img, z.xcor, z.ycor);
                    image(img, z.xcor, z.ycor);
                    p.inventory.remove(b.armor);
                    p.equipped.add(z.armor);
                    reload();
                  }
                  else {
                    if (b.armor == null) {
                      b.armor = new InvSlot(z.armor.item);
                      z.ignore = true;
                      b.ignore = false;
                      img = loadImage("buttonpic.png");
                      eselected = false;
                      invselected = false;
                      b.chosen = false;
                      z.chosen = false;
                      image(img, b.xcor, b.ycor);
                      image(img, b.xcor, b.ycor);
                      image(img, z.xcor, z.ycor);
                      image(img, z.xcor, z.ycor);
                      p.inventory.add(b.armor);
                      reload();
                      
                    }
                    else {
                      if (z.armor.item.getClass().getName().equals(b.armor.item.getClass().getName())) {
                        if (b.ignore) {
                          z.ignore = true;
                          b.ignore = false;
                        } 
                        else { 
                          if (z.ignore) {
                            b.ignore = true;
                            z.ignore = false;
                          }
                        }
                        Armor tmp;
                        tmp = z.armor.item;
                        z.armor.item = b.armor.item;
                        b.armor.item = tmp;
                        img = loadImage("buttonpic.png");
                        eselected = false;
                        invselected = false;
                        b.chosen = false;
                        z.chosen = false;
                        image(img, b.xcor, b.ycor);
                        image(img, b.xcor, b.ycor);
                        image(img, z.xcor, z.ycor);
                        image(img, z.xcor, z.ycor);
                        reload();
                      }
                    }
                  }
                }
              }
            }
            else {
              img = loadImage("buttonpicyellow.png");
              image(img, b.xcor, b.ycor);
              reload();
              invselected = true;
              b.chosen = true;
              break;
            }
          }
          if (b.chosen && invselected && b.isClicked(x, y)) {
            img = loadImage("buttonpic.png");
            image(img, b.xcor, b.ycor);
            image(img, b.xcor, b.ycor);
            reload();
            invselected = false;
            b.chosen = false;
            break;
          }
          j++;
        }
        int q = 0;
        for (Button b : equippedmenu) {
          if (!eselected && b.isClicked(x, y)) {
            if (invselected) {
              for (Button z : bagmenu) {
                if (z.chosen) {
                  if (z.armor == null) {
                    z.armor = new InvSlot(b.armor.item);
                    b.ignore = true;
                    z.ignore = false;
                    img = loadImage("buttonpic.png");
                    eselected = false;
                    invselected = false;
                    b.chosen = false;
                    z.chosen = false;
                    image(img, b.xcor, b.ycor);
                    image(img, b.xcor, b.ycor);
                    image(img, z.xcor, z.ycor);
                    image(img, z.xcor, z.ycor);
                    p.inventory.add(z.armor);
                    reload();
                  }
                  else {
                    if (b.armor == null) {
                      b.armor = new InvSlot(z.armor.item);
                      z.ignore = true; 
                      b.ignore = false;
                      img = loadImage("buttonpic.png");
                      eselected = false;
                      invselected = false;
                      b.chosen = false;
                      z.chosen = false;
                      image(img, b.xcor, b.ycor);
                      image(img, b.xcor, b.ycor);
                      image(img, z.xcor, z.ycor);
                      image(img, z.xcor, z.ycor);
                      p.equipped.add(b.armor);
                      reload();
                    }
                    else {
                      if (z.armor.item.getClass().getName().equals(b.armor.item.getClass().getName())) {
                        /*if (b.ignore) {
                          z.ignore = true;
                          b.ignore = false;
                        }
                        else {
                          if (z.ignore) {
                            b.ignore = false;
                            z.ignore = true;
                          }
                        }*/
                        Armor tmp;
                        tmp = (Armor)z.armor.item;
                        z.armor.item = b.armor.item;
                        b.armor.item = tmp;
                        img = loadImage("buttonpic.png");
                        eselected = false;
                        invselected = false;
                        b.chosen = false;
                        z.chosen = false;
                        image(img, b.xcor, b.ycor);
                        image(img, b.xcor, b.ycor);
                        image(img, z.xcor, z.ycor);
                        image(img, z.xcor, z.ycor);
                        b.ignore = false;
                        p.equipped.set(q,b.armor);
                        reload();
                      }
                    }
                  }
                }
              }
            }
            else {
              img = loadImage("buttonpicyellow.png");    
              image(img, b.xcor, b.ycor);
              reload();
              eselected = true;
              b.chosen = true;
              break;
            }
          }
          if (b.chosen && eselected && b.isClicked(x, y)) {
            img = loadImage("buttonpic.png");
            image(img, b.xcor, b.ycor);
            image(img, b.xcor, b.ycor);
            reload();
            eselected = false;
            b.chosen = false;
            break;
          }
          q++;
        }
      }
    }
    else {
      ready = true;
    }
  }

  public void reload() {
    int x=0;
    int tmp = -1;
    float howlong;
    for (InvSlot m : p.equipped) { 
      equippedmenu.get(x).armor = p.equipped.get(x);
      if (((Armor)(m.getPiece())).name.equals("") || equippedmenu.get(x).ignore) {
        text("EMPTY", equippedmenu.get(x).xcor+20, equippedmenu.get(x).ycor+25);
      }
      else {
        bagmenu.get(x).armor = m;
        howlong = (1.0 * ((Armor)(m.getPiece())).name.length()) / 13.0;
        text(((Armor)(m.getPiece())).name, equippedmenu.get(x).xcor+20, equippedmenu.get(x).ycor+25);
        if (howlong <= 1.0) {
          text(((Armor)(m.getPiece())).name, equippedmenu.get(x).xcor+20, equippedmenu.get(x).ycor+25);
        }
        if (howlong <= 2.0 && howlong > 1.0) {
          text(((Armor)(m.getPiece())).name.substring(0, 12), equippedmenu.get(x).xcor+20, equippedmenu.get(x).ycor+25);
          text(((Armor)(m.getPiece())).name.substring(12), equippedmenu.get(x).xcor+20, equippedmenu.get(x).ycor+35);
        }
        if (howlong <= 3.0 && howlong > 2.0) {
          text(((Armor)(m.getPiece())).name.substring(0, 12), equippedmenu.get(x).xcor+20, equippedmenu.get(x).ycor+25);
          text(((Armor)(m.getPiece())).name.substring(12, 25), equippedmenu.get(x).xcor+20, equippedmenu.get(x).ycor+35);
          text(((Armor)(m.getPiece())).name.substring(25), equippedmenu.get(x).xcor+20, equippedmenu.get(x).ycor+45);
        }
      }
      if (equippedmenu.get(x).ignore) {
        tmp = x;
      }
      x++;
    }
    for (int i = 0; i < p.inventory.size(); i++) {
      if (!bagmenu.get(i).ignore) {
        bagmenu.get(i).armor = p.inventory.get(i);
        howlong = (1.0 * (((Armor)(p.inventory.get(i).getPiece())).name.length())) / 13.0;
        if (howlong <= 1.0) {
          text(((Armor)(p.inventory.get(i).getPiece())).name, bagmenu.get(i).xcor+20, bagmenu.get(i).ycor+25);
        }
        if (howlong <= 2.0 && howlong > 1.0) {
          text(((Armor)(p.inventory.get(i).getPiece())).name.substring(0, 12), bagmenu.get(i).xcor+20, bagmenu.get(i).ycor+25);
          text(((Armor)(p.inventory.get(i).getPiece())).name.substring(12), bagmenu.get(i).xcor+20, bagmenu.get(i).ycor+35);
        }
        if (howlong <= 3.0 && howlong > 2.0) {
          text(((Armor)(p.inventory.get(i).getPiece())).name.substring(0, 12), bagmenu.get(i).xcor+20, bagmenu.get(i).ycor+25);
          text(((Armor)(p.inventory.get(i).getPiece())).name.substring(12, 25), bagmenu.get(i).xcor+20, bagmenu.get(i).ycor+35);
          text(((Armor)(p.inventory.get(i).getPiece())).name.substring(25), bagmenu.get(i).xcor+20, bagmenu.get(i).ycor+45);
        }
      }
      else {
        p.inventory.remove(i);
      }
    }
    if (tmp != -1) {
      if (tmp == 0) {
        p.equipped.set(tmp, new InvSlot(new Helm()));
      }
      if (tmp == 1) {
        p.equipped.set(tmp, new InvSlot( new Gloves()));
      }
      if (tmp == 2) {
        p.equipped.set(tmp, new InvSlot( new Body()));
      }
      if (tmp == 3) {
        p.equipped.set(tmp, new InvSlot(new Pants()));
      }
      if (tmp == 4) {
        p.equipped.set(tmp, new InvSlot( new Shoes()));
      }
      if (tmp == 5) {
        p.equipped.set(tmp, new InvSlot( new Accessory()));
      }
      if (tmp == 6) {
        p.equipped.set(tmp, new InvSlot(new Weapon()));
      }
    }
    /*for (InvSlot h : p.inventory) {
     if (h ==
     }*/
  }
}

